package com.paishen.common.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.paishen.common.LibCommon;

/**
 * 网络环境检测工具
 * Created by peng on 2015/12/2.
 */
public class NetworkUtil {
    // ConnectivityManager.TYPE_NONE = -1;
    public final static int NONE = -1;
//    public final static int WIFI = 1;
//    public final static int MOBILE = 2;

    @Deprecated
    public static int getNetworkState() {
        if(LibCommon.appContext() == null) {
            LogUtil.e("NetworkUtil getNetworkState context empty");
            return -1;
        }
        ConnectivityManager connManager = (ConnectivityManager) LibCommon.appContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        // GPRS
        NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
            return ConnectivityManager.TYPE_MOBILE;
        }
        // WIFI
        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
            return ConnectivityManager.TYPE_WIFI;
        }

        return NONE;
    }

    public static int getNetworkState6(){
        if(LibCommon.appContext() == null) {
            LogUtil.e("NetworkUtil getNetworkState6 context empty");
            return -1;
        }
        ConnectivityManager connManager = (ConnectivityManager) LibCommon.appContext().getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            NetworkInfo.State state = activeNetworkInfo.getState();
            if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                return activeNetworkInfo.getType();
            }
        }
        return NONE;
    }

    public static boolean isNoInternet() {
        return NONE == getNetworkState6();
    }

    @Deprecated
    public static boolean isWiFiOpened() {
        return ConnectivityManager.TYPE_WIFI == getNetworkState();
    }

    @Deprecated
    public static boolean isGPRSOpened() {
        return ConnectivityManager.TYPE_MOBILE == getNetworkState();
    }
}
